using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using OgilvyOne.Model;
using OgilvyOne.IDAL;
using OgilvyOne.Utility;
using System.Collections;

namespace OgilvyOne.SqlServerDAL
{
    public class Common
    {
        // Methods
        public string ConString(string content, string seachKey, int con_id)
        {
            string str = string.Empty;
            string cmdText = " SELECT replace('" + StringUtil.ReplaceHtmlCode(content) + "','" + StringUtil.ReplaceHtmlCode(seachKey) + "','<span style=color:red;font-weight:bold;>" + StringUtil.ReplaceHtmlCode(seachKey) + "</span>') from con_info where con_id=2";
            using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, cmdText, null))
            {
                while (reader.Read())
                {
                    str = reader.GetString(0);
                }
            }
            return str;
        }

        public static string GetChannelDescUrl(int menuId)
        {
            string ret = "/cds/?menu_uid=" + menuId + "&con_id=-1";
            SColumnInfo sci = new SColumnInfo();
            MColumnInfo mci = sci.GetRowByPrimaryKey(menuId,0);
            if (mci != null)
            {
                ret = "/" + mci.FolderName + mci.FileName;
            }
            return ret;
        }

        public static string GetChannelUrl(int menuId)
        {
            SysMenuInfo sysMenuInfoById = new SysMenu().GetSysMenuInfoById(menuId);
            if (null == sysMenuInfoById)
            {
                return "";
            }
            return GetChannelUrl(sysMenuInfoById.Node_ID, sysMenuInfoById.Menu_out_url);
        }

        public static string GetChannelUrl(int menuId, string outUrl)
        {
            int fmenuID = 0;
            try
            {
                fmenuID = Convert.ToInt32(outUrl);
            }
            catch { }

            if (outUrl != "" && fmenuID == 0)
            {
                return outUrl;
            }
            if(fmenuID >0)
                menuId = fmenuID;
            string ret = "/cds/?menu_uid=" + menuId.ToString();
            SColumnInfo sci = new SColumnInfo();
            MColumnInfo mci = sci.GetRowByPrimaryKey(menuId,0);
           
            if (mci != null)
            {
                ret = "/" + mci.FolderName + mci.FileName;
            }
            return ret;
        }

        public static string GetChannelUrlTarget(int menuId)
        {
            SysMenuInfo sysMenuInfoById = new SysMenu().GetSysMenuInfoById(menuId);
            if (null == sysMenuInfoById)
            {
                return "_self";
            }
            return sysMenuInfoById.target;
        }

        public static string GetContentUrl(int menuId, int conId, string conUrl)
        {
            int fmenuID = 0;
            try
            {
                fmenuID = Convert.ToInt32(conUrl);
            }
            catch { }

            if (conUrl != "" && fmenuID == 0)
            {
                return conUrl;
            }
            if (fmenuID > 0)
                menuId = fmenuID;
            string ret = "/cds/?menu_uid=" + menuId.ToString();
            if (conId < 0)
                conId = 0;
            if (conId > 0)
            {
                ret = "/cds/?menu_uid=" + menuId.ToString() + "&con_id="+conId;
            }
            if (conId == 274)
            {
            }
            SColumnInfo sci = new SColumnInfo();
            MColumnInfo mci = sci.GetRowByPrimaryKey(menuId, conId);
            if (mci != null)
            {
                ret = "/" + mci.FolderName + mci.FileName;
            }
            return ret;
        }

        public static IList GetLabelConInfo(int parentId)
        {
            IList list = new ArrayList();
            string cmdText = " SELECT menu_id,label_id from lalel_config_info where menu_parent_id=" + parentId + " and status='0' order by sort desc ";
            using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, cmdText, null))
            {
                while (reader.Read())
                {
                    LabelConfigInfo info = new LabelConfigInfo(reader.GetInt32(0), reader.GetInt32(1));
                    list.Add(info);
                }
            }
            return list;
        }

        public static string GetUploadPath(string picName)
        {
            if (picName.Length == 0)
            {
                return "";
            }
            return (FileUtil.GetUploadPath() + picName);
        }

        public static string GetUploadPath(string picName, int type)
        {
            if (picName.Length == 0)
            {
                return "";
            }
            return (FileUtil.GetUploadPath() + picName);
        }

        public static string ShowAsset(int type, string url)
        {
            if ((type == 1) || (type == 4))
            {
                return string.Format("javascript:showbigImage(\"{0}\");", url);
            }
            if (type == 2)
            {
                return string.Format("javascript:showMedia(\"{0}\");", url);
            }
            return url;
        }
    }
}
